<template>
  <div class='container'>
    <div class="app-container">
      <el-card shadow="naver">
        <template #header>
          <el-breadcrumb separator-class="el-icon-arrow-right" v-if="$route.query.id">
            <el-breadcrumb-item >学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{$route.query.name}}</el-breadcrumb-item>
            <el-breadcrumb-item>目录管理</el-breadcrumb-item>
          </el-breadcrumb>
        </template>
        <el-form :inline="true"  ref="requestParameters" :model="data" >
          <!-- <div class="filter-container"> -->
            <el-form-item label="目录名称"  style="margin-right: 50px">
              <el-input
              v-model="data.directoryName"
                style="width: 200px;"
                class="filter-item"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态" >
              <el-select
                class="filter-item"
                v-model="data.state"
              >
              <el-option :value="1" label="启用"></el-option>
              <el-option :value="0" label="禁用"></el-option>
              </el-select>
              <el-button class="filter-item" size="small"  @click="resetForm" style="margin-left:15px">清除</el-button>
              <el-button
                class="filter-item"
                size="small"
                type="primary"
                @click="handleFilter"
              >搜索</el-button>
            </el-form-item>
            <el-form-item style="float:right">

              <el-button icon="el-icon-back" type="text" size="small" v-if="$route.query.id" @click="$router.push('/subjects/list')">返回学科</el-button>
              <el-button
              class="filter-item fr"
              size="small"
              style="margin-left: 10px;"
              @click="handleCreate"
              type="success"
              icon="el-icon-edit"
            >新增目录</el-button>
            </el-form-item>

          <!-- </div> -->
        </el-form>
        <el-alert :title="title" show-icon type="info" :closable="false" class="alert"></el-alert>
        <!-- table数据 -->
        <el-table :data="list" size="small" style="width:100%" highlight-current-row :header-cell-style="{'background':'#fafafa','text-align': 'center'}" :cell-style="{ 'text-align': 'center'}">
          <el-table-column label="序号" width="50px">
            <template #default="{$index}">
              {{ $index + 1}}
            </template>
          </el-table-column>
          <el-table-column label="所属学科" prop="subjectName"></el-table-column>
          <el-table-column label="目录名称" prop="directoryName"></el-table-column>
          <el-table-column label="创建者" prop="username"></el-table-column>
          <el-table-column label="创建日期" prop="addDate">
          </el-table-column>
          <el-table-column label="面试题数量" prop="totals" width="100px"></el-table-column>
          <el-table-column label="状态" prop="state">
            <template #default="{row}">
              {{row.state? '已启用' : '已禁用'}}
            </template>
          </el-table-column>
          <el-table-column label="操作"  min-width="100">
           <template #default="{row}">
            <el-button type="text" size="small" style="margin-right:15px" @click="handleabled(row.id,row.state)">{{!row.state?"启用":"禁用"}}</el-button>
            <el-button type="text" size="small" style="margin-right:15px" @click="edit(row.id,row.directoryName,row.subjectName)" :disabled="row.state?true:false">修改</el-button>
            <el-button type="text" size="small" @click="del(row.id)" :disabled="row.state?true:false">删除</el-button>
           </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-row style="margin-top:25px">
          <el-pagination
          class="fr"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="data.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="data.pagesize"
          :total="counts"
          layout=" prev, pager, next,sizes, jumper"
          >
        </el-pagination>
        </el-row>
      </el-card>
    </div>
   <DirectorysAdd ref="directorysadd"></DirectorysAdd>
  </div>
</template>

<script>
import { list, remove, changeState } from '../../api/hmmm/directorys'
import DirectorysAdd from '../components/directorys-add'
export default {
  components: {
    DirectorysAdd
  },
  data () {
    return {
      data: {
        page: 1,
        pagesize: 10,
        state: null,
        directoryName: '',
        subjectID: this.$route.query.id
      },
      list: [],
      counts: 0,
      showdialog: false,
      flag: true
    }
  },
  computed: {
    title () {
      return `数据一共${this.counts}条`
    }
  },
  created () {
    this.getList()
  },
  watch: {
    '$route.query.id' (val) {
      if (!val) {
        this.data.subjectID = null
        this.getList()
      }
    }
  },
  methods: {
    async handleabled (id, state) {
      // const state = this.list.find(ele => ele.id === id).state
      // this.list.find(ele => ele.id === id).state = !state
      this.list.forEach(ele => { ele.state = ele.id === id ? !ele.state : ele.state })
      await changeState({ id, state: +!state })
      this.$message.success('操作成功')
    },
    async getList () {
      const res = await list(this.data)
      console.log(res.data)
      this.counts = res.data.counts
      this.list = res.data.items
      this.list.forEach(ele => { ele.addDate = this.transTime(ele.addDate) })
    },
    transTime (isoDate) {
      if (!isoDate) return '-'
      const date = new Date(isoDate)
      return date.toLocaleString().replace(/\//g, '-')
    },
    resetForm () {
      this.data.state = null
      this.data.directoryName = ''
    },
    handleSizeChange (val) {
      this.data.pagesize = val
      this.page = 1
      this.getList()
    },
    handleCurrentChange (val) {
      this.data.page = val
      this.getList()
    },
    handleFilter () {
      this.getList()
    },
    handleCreate () {
      this.showdialog = true
    },
    edit (id, directoryName, subjectName) {
      this.$refs.directorysadd.edit(id, directoryName, subjectName)
      this.handleCreate()
    },
    async del (id) {
      try {
        await this.$confirm('此操作将永久删除用户 ' + ', 是否继续?', '提示', {
          type: 'warning'
        })
        await remove({ id })
        this.$message.success('删除成功')
        this.getList()
      } catch {
        // console.log(111)
      }
    }
  }
}
</script>

<style scoped lang='less'></style>
